import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-07-22
 * Time: 20:41
 */
class Solution12 {
    public int[] advantageCount(int[] nums1, int[] nums2) {
        int n = nums1.length;

        //创建下标数组
        Integer[] index = new Integer[n];

        for(int i = 0; i < n; i++) {
            index[i] = i;
        }

        //排序
        Arrays.sort(nums1);
        Arrays.sort(index,(i,j)->{
            return nums2[i]-nums2[j];
        });

        int[] ret = new int[n];
        int left = 0,right = n-1;
        for(int x : nums1) {
            if(x > nums2[index[left]]) {
                ret[index[left++]] = x;
            }else {
                ret[index[right--]] = x;
            }
        }
        return ret;
    }
}
